/**
 * 
 */
package com.google.code.onlineshopping4j.crud.rest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.code.onlineshopping4j.exception.OnlineShoppingException;
import com.google.code.onlineshopping4j.exception.ProductNotFoundExcpetion;
import com.google.code.onlineshopping4j.service.DashBoardService;

/**
 * @author TelmilaJ
 *
 */
@Controller
@RequestMapping(ProductController.PRODUCT_REQUEST_GLOBAL_MAPPING)
public class ProductController {
	
	public static final String PRODUCT_REQUEST_GLOBAL_MAPPING = "/product";
	public static final String PRODUCT_REQUEST_MAPPING_BY_ID = "/{productId}";
	public static final String PRODUCT_LIST_REQUEST_MAPPING = "/list";
	
	public static final String PRODUCT_MODEL_ATTRIBUTE_NAME = "product";
	public static final String PRODUCT_VIEW_NAME = "product";
	
	public static final String PRODUCT_LIST_MODEL_ATTRIBUTE_NAME = "products";
	public static final String PRODUCT_LIST_VIEW_NAME = "products";
	
	@Autowired
	private DashBoardService dashBoardService;
	
	@RequestMapping(value=PRODUCT_REQUEST_MAPPING_BY_ID, method=RequestMethod.GET)
	public String getProductById(@PathVariable Long productId, ModelMap model) throws ProductNotFoundExcpetion {
		model.addAttribute(PRODUCT_MODEL_ATTRIBUTE_NAME, dashBoardService.getProduct(productId));
		return PRODUCT_VIEW_NAME;
	}
	
	@RequestMapping(value=PRODUCT_LIST_REQUEST_MAPPING, method=RequestMethod.GET)
	public String getAllProduct(ModelMap model) throws OnlineShoppingException {
		model.addAttribute(PRODUCT_LIST_MODEL_ATTRIBUTE_NAME, dashBoardService.getAllProduct());
		return PRODUCT_LIST_VIEW_NAME;
	}
}
